package com.huaqin.obeacons;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.huaqin.obeacons.utils.Clock;

public class ClockItemView extends BaseHolderView<Clock> implements
		View.OnClickListener {
	private View mInBar;
	private OnClickStatusViewListener mListener;
	private View mOutBar;
	private int mPosition;
	private ImageView mStatusView;
	private TextView mTimeInView;
	private TextView mTimeOutView;
	private SimpleDateFormat mClockTimeformat;
	private Date mDate = new Date();

	public ClockItemView(Context paramContext) {
		super(paramContext);
	}

	private Date getDate() {
		if (mDate == null)
			mDate = new Date();
		return mDate;
	}

	@SuppressLint("SimpleDateFormat")
	private String getTimeFormat(long paramLong) {
		if (mClockTimeformat == null)
			mClockTimeformat = new SimpleDateFormat("HH:mm:ss");
		getDate().setTime(paramLong);
		return mClockTimeformat.format(mDate);
	}

	public void bindItemView(View paramView, ViewGroup paramViewGroup,
			int paramInt, Clock paramClock, Object[] paramArrayOfObject) {
		this.mPosition = paramInt;
		if (this.mListener != null) {
			this.mStatusView.setTag(paramClock);
			this.mStatusView.setOnClickListener(this);
		}
		switch (paramClock.status.intValue()) {
		case 0:
			// this.mTimeInView
			// .setText(getTimeFormat(paramClock.time.longValue()));
			this.mTimeInView.setText(paramClock.timeString);
			this.mTimeInView.setVisibility(View.VISIBLE);
			this.mInBar.setVisibility(View.VISIBLE);
			this.mOutBar.setVisibility(View.GONE);
			this.mTimeOutView.setVisibility(View.GONE);
			this.mStatusView.setImageResource(R.drawable.status_out);
			this.mStatusView.setVisibility(View.VISIBLE);
			break;
		case 1:
			// this.mTimeOutView
			// .setText(getTimeFormat(paramClock.time.longValue()));
			this.mTimeOutView.setText(paramClock.timeString);
			this.mTimeOutView.setVisibility(View.VISIBLE);
			this.mOutBar.setVisibility(View.VISIBLE);
			this.mInBar.setVisibility(View.GONE);
			this.mTimeInView.setVisibility(View.GONE);
			this.mStatusView.setImageResource(R.drawable.status_in);
			this.mStatusView.setVisibility(View.VISIBLE);
			break;
		}
	}

	public View onAttach(Context paramContext) {
		View localView = LayoutInflater.from(paramContext).inflate(
				R.layout.clock_list_item, null);
		this.mTimeInView = ((TextView) localView.findViewById(R.id.clock_text1));
		this.mTimeOutView = ((TextView) localView
				.findViewById(R.id.clock_text2));
		this.mOutBar = localView.findViewById(R.id.clock_bar2);
		this.mInBar = localView.findViewById(R.id.clock_bar1);
		this.mStatusView = ((ImageView) localView
				.findViewById(R.id.clock_image));
		return localView;
	}

	public void onClick(View paramView) {
		if (this.mListener != null) {
			Clock localClock = (Clock) paramView.getTag();
			this.mListener.onClick(this.mPosition, localClock);
		}
	}

	public void setListener(
			OnClickStatusViewListener paramOnClickStatusViewListener) {
		this.mListener = paramOnClickStatusViewListener;
	}

	public static abstract interface OnClickStatusViewListener {
		public abstract void onClick(int paramInt, Clock paramClock);
	}
}
